package edu.nf.supermarket.web.controller.app;

import edu.nf.supermarket.base.BaseController;
import edu.nf.supermarket.entity.Commodity;
import edu.nf.supermarket.entity.Orders;
import edu.nf.supermarket.result.ResultVO;
import edu.nf.supermarket.service.CommodityService;
import edu.nf.supermarket.service.OrderService;
import edu.nf.supermarket.util.OrderNOGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author yjj
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CommodityService commodityService;



    /**
     * 查询用户购物车中的商品
     *
     * @param userId
     * @return
     */
    @RequestMapping("/findCommodityByUserId")
    public ResultVO<Orders> findCommodityByUserId(@RequestParam("userId") Integer userId) {
        Orders orders = orderService.findCommodityByUserIdNoDelete(userId);

        for (Commodity commodity : orders.getCommodityList()) {
            commodity.setCommodityPicture(commodityService.findCommodityById(commodity.getCommodityId())
                    .getCommodityPicture());
        }
        return success(orders);
    }

    /**
     * 往购物车中添加商品方法
     */
    @PostMapping("/addCommodityToCart")
    public ResultVO<Commodity> addCommodityToCart(@RequestParam("userId") Integer userId,
                                                  @RequestParam("productId") Integer commodityId,
                                                  @RequestParam("quantity") Integer commodityNum,
                                                  @RequestParam("selectedSpecs") String[] selectedSpecs
                                                  ) {
        Orders commodityByUserId = orderService.findCommodityByUserIdNoDelete(userId);
        //判断购物车是否存在商品
        if (commodityByUserId == null) {
            String orderId = orderService.findOrderId(userId);
            orderService.addCommodityToCart(orderId, commodityId, commodityNum, selectedSpecs[0], selectedSpecs[1]);
            return success();
        }
        //判断购物车中是否存在该商品，存在则修改数量
        for (Commodity commodity : commodityByUserId.getCommodityList()) {
            if (commodity.getCommodityId().equals(commodityId)) {
                orderService.updateCommodityNum(commodityByUserId.getOrderId(), commodity.getCommodityId(), commodity.getCommodityNum() + commodityNum);
                return success();
            }
        }
        orderService.addCommodityToCart(commodityByUserId.getOrderId(), commodityId, commodityNum, selectedSpecs[0], selectedSpecs[1]);
        return success();
    }

    /**
     * 修改购物车中商品数量
     */
    @GetMapping("/updateCommodityNum")
    public ResultVO<Commodity> updateCommodityNum(@RequestParam("orderId") String orderId,
                                                  @RequestParam("productId") Integer commodityId,
                                                  @RequestParam("quantity") Integer commodityNum) {
        orderService.updateCommodityNum(orderId, commodityId, commodityNum);
        return success();
    }

    /**
     * 删除购物车中商品
     */
    @PostMapping("/deleteCommodityFromCart")
    public ResultVO<Commodity> deleteCommodityFromCart(@RequestParam("orderId") String orderId,
                                                       @RequestParam("productId") Integer commodityId) {
        orderService.deleteCommodityFromCart(orderId, commodityId);
        return success();
    }

    /**
     * 结算方法
     */
    @PostMapping("/buyCommodity")
    public ResultVO<String> buyCommodity(@RequestParam("userId") Integer userId,
                                         @RequestBody Map<Integer, Commodity> commodity) {
        //生成订单编号
        String orderId = OrderNOGenerator.generateOrderNO();
        //生成订单
        Orders orders = new Orders();
        orders.setOrderId(orderId);
        orders.setCreateTime(LocalDateTime.now());
        orders.setStatus((byte) 0);
        orderService.generateOrder(userId, orders, commodity);
        return success(orderId);
    }
}
